package com.test.clyq.action;

import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.test.clyq.entity.Users;
import com.test.clyq.entity.UsersLog;
import com.test.clyq.service.UsersLogService;
import com.test.json.JsonUtil;
import com.test.util.DateUtils;
import com.test.util.FormatExceptionStack;
import com.test.util.ItemUtil;
import com.test.util.Pagination;
import com.test.util.SessionUtil;
import com.test.webapp.action.BaseAction;

/**
 * UsersLogAction. <br>
 * 用户日志管理.
 * <p>
 * Copyright: Copyright (c) 2010-4-17 下午04:52:10
 * <p>
 * Company: 北京宽连十方数字技术有限公司
 * <p>
 * Author: liuxl@c-platform.com
 * <p>
 * Version: 1.0
 * <p>
 */
public class UsersLogAction extends BaseAction {

	/**
	 * 删除用户日志
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws IOException
	 */
	public ActionForward delUsersLog(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
	        throws IOException {
		this.log.info("删除用户日志start");
		String uId = request.getParameter("uId");
		UsersLogService usersLogService = (UsersLogService) getBean("usersLogService");
		Users user = SessionUtil.getUserFromSession(request);
		try {
			boolean bl = usersLogService.delUsersLogById(Long.valueOf(uId), user);
			StringBuilder sb = new StringBuilder();
			sb.append("{");
			if (bl) {
				sb.append("success:true,");
			} else {
				sb.append("error:error");
				request.setAttribute("mark", "<script>alert('删除失败!');</script>");
			}
			sb.append("}");
			String dd = sb.toString();
			this.log.debug("删除用户日志成功");
			this.log.info("删除用户日志end");
			print(response, dd, null, null);
		}
		catch (Exception e) {
			this.log.error("删除用户日志:失败 " + FormatExceptionStack.formatException(e));
			print(response, false);
		}
		return null;

	}

	/**
	 * 获得用户日志列表
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return 用户日志列表
	 * @throws IOException
	 */
	public ActionForward usersLogList(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
	        throws IOException {
		this.log.info("获得用户日志列表start");
		UsersLogService usersLogService = (UsersLogService) getBean("usersLogService");
		try {
			String userId = request.getParameter("usersId");
			String sTime = request.getParameter("startTime");
			String eTime = request.getParameter("endTime");
			String start = request.getParameter("start");
			if (start != null && !"".equals(start)) {

			} else {
				start = 0 + "";
			}
			int currpageNo = 1;
			if (Integer.parseInt(start) / 20 != 0) {
				currpageNo = Integer.parseInt(start) / 20 + 1;// 当前页码
			}
			Pagination pagination = new Pagination(currpageNo, 20);// 设置分页参数（页码和每页显示记录条数）
			List<UsersLog> list = null;
			if (userId != null && !"".equals(userId)) {
				if (sTime != null && !"".equals(sTime) && eTime != null && !"".equals(eTime)) {
					list = usersLogService.usersLogListBySearch(Long.valueOf(userId), DateUtils.stringToLong(sTime), DateUtils.stringToLong(eTime),
					                                            pagination); // 获取用户日志列表
				} else if (sTime != null && !"".equals(sTime)) {
					list = usersLogService.usersLogListBySearch(Long.valueOf(userId), DateUtils.stringToLong(sTime), Long.valueOf(-1), pagination); // 获取用户日志列表
				} else {
					list = usersLogService.usersLogListBySearch(Long.valueOf(userId), Long.valueOf(-1), Long.valueOf(-1), pagination); // 获取用户日志列表
				}
			} else if (sTime != null && !"".equals(sTime) && eTime != null && !"".equals(eTime)) {
				list = usersLogService.usersLogListBySearch(Long.valueOf(-1), DateUtils.stringToLong(sTime), DateUtils.stringToLong(eTime),
				                                            pagination); // 获取用户日志列表

			} else if (sTime != null && !"".equals(sTime)) {
				list = usersLogService.usersLogListBySearch(Long.valueOf(-1), DateUtils.stringToLong(sTime), Long.valueOf(-1), pagination); // 获取用户日志列表
			} else {
				list = usersLogService.usersLogList(pagination); // 获取用户日志列表
			}
			if (list != null && !list.isEmpty()) {
				for (UsersLog ul : list) {
					ul.setOperateDataTypeStr(ItemUtil.TYPENAMEMAP.get(ul.getOperateDataType().intValue()));
					ul.setOperateStatusStr(ItemUtil.STATUSMAP.get(ul.getOperateStatus().intValue()));
					ul.setOperateTypeStr(ItemUtil.OPERATORMAP.get(ul.getOperateType().intValue()));
				}
			}
			int totalCount = pagination.getTotalRows();
			String jsonStr = JsonUtil.getJsonStr(list, new String[] { "usersLogId", "usersId", "usersLoginName", "operateTime", "operateTypeStr",
			        "operateDataTypeStr", "operateStatusStr", "loginWebsiteId" }, new String[] { "operateTime" }, totalCount);
			this.log.debug("获得用户日志列表成功");
			this.log.info("获得用户日志列表end");
			print(response, jsonStr, null, null);
		}
		catch (Exception e) {
			this.log.error("获得用户日志:失败 " + FormatExceptionStack.formatException(e));
			print(response, false);
		}
		return null;
	}

	/**
	 * 显示用户日志
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws IOException
	 */
	public ActionForward viewLog(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws IOException {
		String uId = request.getParameter("uId");
		UsersLogService usersLogService = (UsersLogService) getBean("usersLogService");
		Users user = SessionUtil.getUserFromSession(request);
		try {
			boolean bl = usersLogService.delUsersLogById(Long.valueOf(uId), user);
			StringBuilder sb = new StringBuilder();
			sb.append("{");
			if (bl) {
				sb.append("success:true,");
			} else {
				sb.append("error:error");
				request.setAttribute("mark", "<script>alert('删除失败!');</script>");
			}
			sb.append("}");
			String dd = sb.toString();
			print(response, dd, null, null);
		}
		catch (Exception e) {
			this.log.error("显示用户日志:失败 " + FormatExceptionStack.formatException(e));
			print(response, false);
		}
		return null;

	}
}
